<?php 
	//边界点位
	$log1=116.802993;
	$log2=116.807606;
	$lat3=39.952599;
	$lat4=39.958356;
	$log5=116.802049;
	$log6=116.805246;
	$lat7=39.958562;
	$lat8=39.961744;
	$areaSouth = 0;
	$areaNorth = 0;
	$outSideNow = 0;
	$northMax = 5000;
	$southMax = 7000;
	$d = new Datetime();
	$year = $d->format('Y');
	$month = $d->format('m');
	$day = $d->format('d');
	$hour = $d->format('H');
	$minute = $d->format('i');
	while($minute % 5 != 0){
		// echo $minute,",";
		$minute--;
	}
	// echo $minute;
	// $minute = (DateTime)($minute);
	$seconds = $d->format('s');
	header("content-type:text/html;charset=utf-8");
	include('connect.php');
	// $timehm = $hour+$minute;
	$sql = "select log,lat from userposition where TIME like '{$hour}:{$minute}'";// .$hour":".$minute"'
	$result = $db->query($sql);
	while ($row = $result->fetch_assoc()) {
		if($row['log']>= $log1 && $row['log'] <= $log2 && $row['lat']>= $lat3 && $row['lat']<= $lat4){
			$areaSouth++;
		}else if($row['log']>= $log5 && $row['log'] <= $log6 && $row['lat']>= $lat7 && $row['lat']<= $lat8){
			$areaNorth++;
		}else{
			$outSideNow++;
		}
	}
	// echo $areaSouth, $areaNorth, $outSideNow;
	function areaState($value,$maxValue){
		// echo "$value , $maxValue ";
			$valuePercen =(int) ($value/$maxValue);
			if($valuePercen <= 60){
				$areaState = "正常";
			}else if($valuePercen <= 80){
				$areaState = "拥挤";
			}else if($valuePercen <= 100){
				$areaState = "饱和";
			}else{
				$areaState = "超载";
			}
			return $areaState;
	}
	$sqlReadNorthNow = "select nowValue from northinfo order by time desc limit 1";
	$result = $db->query($sqlReadNorthNow);
	$northLast = mysqli_fetch_assoc($result)['nowValue'];
	$northState = areaState($areaNorth,$northMax);

	$sqlReadSouthNow = "select nowValue from southinfo order by time desc limit 1";
	$result = $db->query($sqlReadSouthNow);
	$southLast = mysqli_fetch_assoc($result)['nowValue'];
	$southState = areaState($areaSouth,$southMax);
	$date = $d->format('Y-m-d H:i:s');
	$sqlInsertNorthinfo  = "insert into northinfo (time,nowValue,lastValue,state) value ('{$date}',{$areaNorth},{$northLast},'{$northState}')";
	$sqlInsertSouthinfo  = "insert into southinfo (time,nowValue,lastValue,state) value ('{$date}',{$areaSouth},{$southLast},'{$southState}')";
	// echo "$sqlInsertNorthinfo                     $sqlInsertSouthinfo";
	$db->query($sqlInsertNorthinfo);
	$db->query($sqlInsertSouthinfo);
 ?>